﻿using ITSM.Model;
using ITSM.Model.Entity;
using ITSM.Model.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ITSM.Service
{
    public class KnowledgeBase : BaseDB, IKnowledgeBase
    {
        public bool AddCTR(string RYD_Id)
        {
            return db.Updateable<RYD_KnowledgeBase>().SetColumns(res=>new RYD_KnowledgeBase() { RYD_CTR= res.RYD_CTR + 1 }).Where(res => res.RYD_Id == RYD_Id).ExecuteCommand()>0;
        }

        public string AddKnowledgeBase(M_DemandOperate operate)
        {
            try
            {
                var demandInfo = db.Queryable<RYD_DemandList>().Where(res => res.RYD_DemandId == operate.RYD_DemandId && res.RYD_Company == operate.RYD_Company).First();
                RYD_KnowledgeBase rYD_KnowledgeBase = new()
                {
                    RYD_Id = Guid.NewGuid().ToString(),
                    RYD_Company = operate.RYD_Company,
                    RYD_DemandDetails = demandInfo.RYD_DemandDetails,
                    RYD_Files = demandInfo.RYD_Files,
                    RYD_Reason = operate.RYD_Reason,
                    RYD_Solution = operate.RYD_Solution,
                    RYD_CTR = 0,
                    RYD_Useful = 0,
                    RYD_IsDelete = 1,
                    RYD_CreateTime = DateTime.Now,
                    RYD_CreateBy = operate.RYD_UserId
                };
                return db.Insertable(rYD_KnowledgeBase).ExecuteCommand() > 0 ? rYD_KnowledgeBase.RYD_Id : "";

            }
            catch (Exception)
            {
                return "";
            }
        }

        public bool AddUseful(string RYD_Id)
        {
            return db.Updateable<RYD_KnowledgeBase>().SetColumns(res => new RYD_KnowledgeBase() { RYD_Useful = res.RYD_Useful + 1 }).Where(res => res.RYD_Id == RYD_Id).ExecuteCommand() > 0;
        }

        public bool AddUseless(string RYD_Id)
        {
            return db.Updateable<RYD_KnowledgeBase>().SetColumns(res => new RYD_KnowledgeBase() { RYD_Useless = res.RYD_Useless + 1 }).Where(res => res.RYD_Id == RYD_Id).ExecuteCommand() > 0;
        }

        public List<RYD_KnowledgeBase> QueryKnowledgeBase(M_QueryKnowledgeBase queryKnowledgeBase)
        {
            try
            {
                var exp = Expressionable.Create<RYD_KnowledgeBase>()
                       .AndIF(!string.IsNullOrWhiteSpace(queryKnowledgeBase.RYD_DemandDetails), res =>
                           res.RYD_DemandDetails.Contains(queryKnowledgeBase.RYD_DemandDetails)
                           || res.RYD_Reason.Contains(queryKnowledgeBase.RYD_DemandDetails)
                       ).And(res =>
                           res.RYD_IsDelete == 1
                       );
                return db.Queryable<RYD_KnowledgeBase>().Where(exp.ToExpression()).OrderByDescending(res => new { res.RYD_Useful, res.RYD_CTR }).Take(queryKnowledgeBase.Num).ToList();
            }
            catch (Exception)
            {
                return new List<RYD_KnowledgeBase>();
            }
        }
    }
}
